<template>
  <div class="wallpaper-modal">
    <div class="preview">
      <img :src="wallpaper" class="preview" alt />
      <div class="desc">
        <p>充满屏幕</p>
      </div>
    </div>
    <div class="pic-list">
      <template v-for="(src, index) in imgs">
        <div
          class="select-img"
          :key="index"
          :class="{
            active: src === wallpaper
          }"
        >
          <img :src="src" alt @click="changePaper(src)" />
        </div>
      </template>
    </div>
    <div class="operate">
      <IconConfigProvider class="icon" color="white" size="20">
        <button class="add" @click="choose">
          <input
            type="file"
            accept=".png, .jpeg, .jpg"
            ref="upload"
            style="display: none"
            @change="uploadPic"
          />
          <Icon>
            <AddFilled />
          </Icon>上传壁纸
        </button>
        <!-- <button class="del" :disabled="wallpaper" @click="del">
          <Icon>
            <MinusFilled />
          </Icon>
        </button>-->
      </IconConfigProvider>
    </div>

    <div class="error" v-if="error">
      <IconConfigProvider size="24">
        <Icon>
          <WarningAmberRound />
        </Icon>
      </IconConfigProvider>
      {{ error }}
    </div>
  </div>
</template>

<script>
import { AddFilled, WarningAmberRound } from "@v2icons/material"
import { Icon, IconConfigProvider } from "@v2icons/utils"
import api from "@/api"
import { mapActions, mapState } from "vuex"
import storage from "@/utils/storage"
export default {
  data() {
    return {
      imgs: [
        "https://s6-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/61293279e5fed8ee_1629532432523.jpeg",
        "https://s3-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/1c9595c9e34b67d1_1629532355758.jpeg",
        "https://s6-lc.thelarkcloud.com/obj/larkcloud-mgcloud/baas/qc4nsi/350a06dce968edd0_1629598075980.jpeg"
      ],
      error: ""
    }
  },
  computed: {
    ...mapState(["wallpaper"])
  },
  components: {
    AddFilled,
    WarningAmberRound,
    Icon,
    IconConfigProvider
  },
  methods: {
    ...mapActions(["changeWallpaper", "authJudge"]),
    changePaper(src) {
      this.changeWallpaper(src)
    },
    uploadPic() {
      this.error = ""
      this.authJudge().then(() => {
        api
          .uploadWallpaper(this.$refs.upload.files[0])
          .then((result) => {
            this.imgs.push(result.data)
            this.persist()
          })
          .catch((err) => {
            console.error(err)
            this.error = err
          })
      })
    },
    choose() {
      this.$refs.upload.value = ""
      this.$refs.upload.click()
    },
    restore() {
      const list = storage.getItem("wallpaper-list")

      if (list) {
        this.imgs = list
      }
    },

    persist() {
      storage.setItem("wallpaper-list", JSON.stringify(this.imgs))
    }
  },
  mounted() {
    this.restore()
  }
}
</script>

<style lang="less" scoped>
.wallpaper-modal {
  padding: 10px;
}
.preview {
  display: flex;
  align-items: center;
  img {
    border: solid 1px white;
    padding: 10px;
    border-radius: 5px;
    width: 150px;
    // height: 100px;
  }
  margin-bottom: 10px;
  .desc {
    margin-left: 20px;
    p {
      margin-bottom: 10px;
      color: white;
      text-shadow: 0px 0px 5px black;
    }
  }
}
.error {
  display: flex;
  border-radius: 6px;
  padding: 6px;
  align-items: center;
  border: 1px solid #ffccc7;
  font-size: 14px;
  background: #fff2f0;
  color: #ff4d4f;
}
.operate {
  margin-top: 10px;
  .icon {
    button {
      cursor: pointer;
      border: none;
      outline: none;
      padding: 5px;
      border-radius: 4px;
      background-color: #409eff;
      border-color: #409eff;
      display: flex;
      align-items: center;
      justify-content: center;
      color: white;
      width: 100%;
      &:hover {
        background-color: #66b1ff;
        border-color: #66b1ff;
      }
    }
  }
}
.pic-list {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .select-img {
    cursor: pointer;
    position: relative;
    // padding: 2px;
    // margin: 5px;
    margin: 5px;
    height: 80px;
    img {
      height: 100%;
    }
    // height: 90px;
    &.active::before {
      content: "";
      position: absolute;
      left: -4px;
      right: -4px;
      top: -4px;
      bottom: -4px;
      // padding: 5p;
      border: solid 2px rgb(0, 102, 220);
    }
  }
}
</style>
